package com.yinhe.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.system.domain.YhFeedback;
import com.yinhe.system.service.IYhFeedbackService;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.common.core.page.TableDataInfo;

/**
 * 意见反馈Controller
 * 
 * @author yinhe
 * @date 2022-09-26
 */
@RestController
@RequestMapping("/system/feedback")
public class YhFeedbackController extends BaseController
{
    @Autowired
    private IYhFeedbackService yhFeedbackService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询意见反馈列表
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhFeedback yhFeedback, String userName)
    {
        //presentOccupation

        startPage();
        ArrayList<Object> list1 = new ArrayList<>();
        /*if (userName==null||userName.length()==0){*/
            //yhFeedback.setUserId(getUserId());
           // yhFeedback.setDeptId(getDeptId());
        System.out.println(yhFeedback+"999");
            List<YhFeedback> list = yhFeedbackService.selectYhFeedbackList(yhFeedback);
            return getDataTable(list);
      //  }
       /* else {
            SysUser user = userService.selectUserByUserName1(userName);
            if (user==null){
                yhFeedback.setUserId(111111L);
                List<YhFeedback> list = yhFeedbackService.selectYhFeedbackList(yhFeedback);
                return getDataTable(list);
            }
            else if (user!=null){
                yhFeedback.setUserId(user.getUserId());
                yhFeedback.setDeptId(getDeptId());

                List<YhFeedback> list = yhFeedbackService.selectYhFeedbackList(yhFeedback);
                return getDataTable(list);
            }*/



        //return getDataTable(list1);
    }

    //获取用户信息
    @RequestMapping("/info")
    public AjaxResult getInfo(){
       SysUser sysUser =   userService.selectUserById(getUserId());
        YhFeedback yhFeedback = new YhFeedback();
        yhFeedback.setNote1(sysUser.getPhonenumber());
        return AjaxResult.success(yhFeedback);
    }

    //提交之后的方法处理
    @PreAuthorize("@ss.hasPermi('system:feedback:commit')")
    @RequestMapping("/commit/{id}")
    public AjaxResult getCommit(@PathVariable Long id){
        System.out.println(id+"89");
        YhFeedback yhFeedback = yhFeedbackService.selectYhFeedbackById(id);
        if (yhFeedback.getNote2().equals("未反馈")){
            yhFeedback.setNote2("已反馈");
            yhFeedbackService.updateYhFeedback(yhFeedback);
        }
        else if (yhFeedback.getNote2().equals("已反馈")){
            yhFeedback.setNote2("已处理");
            yhFeedbackService.updateYhFeedback(yhFeedback);
        }
        return AjaxResult.success(yhFeedback);
    }

    /**
     * 导出意见反馈列表
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:export')")
    @Log(title = "意见反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhFeedback yhFeedback)
    {
        List<YhFeedback> list = yhFeedbackService.selectYhFeedbackList(yhFeedback);
        ExcelUtil<YhFeedback> util = new ExcelUtil<YhFeedback>(YhFeedback.class);
        util.exportExcel(response, list, "意见反馈数据");
    }

    /**
     * 获取意见反馈详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhFeedbackService.selectYhFeedbackById(id));
    }

    /**
     * 新增意见反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:add')")
    @Log(title = "意见反馈", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    public AjaxResult add(@RequestBody YhFeedback yhFeedback)
    {// 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhFeedback.setUserId(sysUser.getUserId());
        yhFeedback.setDeptId(sysUser.getDeptId());
     /*   yhFeedback.setUserId(getUserId());
        yhFeedback.setDeptId(getDeptId());*/
        //System.out.println(yhFeedback.getProductType()+"77");
        yhFeedback.setNote2("未反馈");
        return toAjax(yhFeedbackService.insertYhFeedback(yhFeedback));
    }

    /**
     * 修改意见反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:edit')")
    @Log(title = "意见反馈", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhFeedback yhFeedback)
    {
        return toAjax(yhFeedbackService.updateYhFeedback(yhFeedback));
    }

    /**
     * 删除意见反馈
     */
    //@PreAuthorize("@ss.hasPermi('system:feedback:remove')")
    @Log(title = "意见反馈", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhFeedbackService.deleteYhFeedbackByIds(ids));
    }
}
